﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VTS.Common;
using VTS.BLL;
using VTS.DAL;

public partial class admin_Modules_Result_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utility.HasLoggedIn())
            Response.Redirect(Constants.LOGIN_PAGE);

        grwKqHocSinh.Attributes.Add("id", "product-table");
        grwKqHocSinh.PageSize = Constants.PAGE_SIZE;
    

        if (!Page.IsPostBack)
        {
            BindDataForControls();
        }
        else
        {
            string deleteId = Request["DeleteId"];

            if (!string.IsNullOrEmpty(deleteId))
            {
                int id = 0;
                int.TryParse(deleteId, out id);

                if (id != 0)
                {
                    KQHocSinhBLL bllKQ = new KQHocSinhBLL();
                    bllKQ.DeleteById(id);
                    Response.Redirect(Constants.DEFAULT_PAGE);
                }
            }

        }
    }

    private void BindDataForPager()
    {
        GridViewRow pagerRow = grwKqHocSinh.BottomPagerRow;
        DropDownList ddlPager = pagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = pagerRow.FindControl("ltrCurrentPage") as Literal;
        Literal ltrTotalPage = pagerRow.FindControl("ltrTotalPage") as Literal;

        for (int i = 1; i <= grwKqHocSinh.PageCount; i++)
        {
            ddlPager.Items.Add(i.ToString());
        }

        ddlPager.SelectedIndex = grwKqHocSinh.PageIndex;
        ltrCurrentPage.Text = (grwKqHocSinh.PageIndex + 1).ToString();
        ltrTotalPage.Text = grwKqHocSinh.PageCount.ToString();
    }

    private void BindDataForControls()
    {
        NamHocBLL bllNamHoc = new NamHocBLL();
        ddlNamHoc.DataSource = bllNamHoc.GetAll();
        ddlNamHoc.DataValueField = "MaNH";
        ddlNamHoc.DataTextField = "NienKhoa";
        ddlNamHoc.DataBind();

        string currentYear = string.Concat(DateTime.Now.Year, " - ", DateTime.Now.Year + 1);
        ListItem item = ddlNamHoc.Items.FindByText(currentYear);
        if (item != null) item.Selected = true;

        LopBLL bllLop = new LopBLL();
        ddlLop.DataSource = bllLop.GetAll();
        ddlLop.DataTextField = "TenLop";
        ddlLop.DataValueField = "MaLop";
        ddlLop.DataBind();
        ddlLop.Items.Insert(0, new ListItem("Tất cả", "0"));
        ddlLop.SelectedIndex = 0;

        BindGridview();
    }

    private void BindGridview()
    {
        KQHocSinhBLL bllKq = new KQHocSinhBLL();
        grwKqHocSinh.DataSource = bllKq.GetByNamAndLopAndHocKy(ddlNamHoc.SelectedValue, ddlLop.SelectedValue, ddlHocKy.SelectedValue);
        grwKqHocSinh.DataBind();
    }

    protected void ddlNamHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        KQHocSinhBLL bllKq = new KQHocSinhBLL();
        grwKqHocSinh.DataSource = bllKq.GetByNamAndLopAndHocKy(ddlNamHoc.SelectedValue, ddlLop.SelectedValue, ddlHocKy.SelectedValue);
        grwKqHocSinh.DataBind();
    }

    protected void ddlLop_SelectedIndexChanged(object sender, EventArgs e)
    {
        KQHocSinhBLL bllKq = new KQHocSinhBLL();
        grwKqHocSinh.DataSource = bllKq.GetByNamAndLopAndHocKy(ddlNamHoc.SelectedValue, ddlLop.SelectedValue, ddlHocKy.SelectedValue);
        grwKqHocSinh.DataBind();
    }

    protected void ddlHocKy_SelectedIndexChanged(object sender, EventArgs e)
    {
        KQHocSinhBLL bllKq = new KQHocSinhBLL();
        grwKqHocSinh.DataSource = bllKq.GetByNamAndLopAndHocKy(ddlNamHoc.SelectedValue, ddlLop.SelectedValue, ddlHocKy.SelectedValue);
        grwKqHocSinh.DataBind();
    }

    protected void grwKqHocSinh_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            KQHocSinh kqHs = e.Row.DataItem as KQHocSinh;
            
            if (kqHs != null)
            {
                e.Row.Cells[1].Text = new NamHocBLL().GetById(kqHs.NamHoc).NienKhoa;
                e.Row.Cells[2].Text = !kqHs.HocKy.HasValue || !kqHs.HocKy.Value ? "1" : "2";
                e.Row.Cells[3].Text = new LopBLL().GetById(kqHs.Lop).TenLop;
                
                LinkButton lbtXoa = e.Row.Cells[4].FindControl("lbtXoa") as LinkButton;
                lbtXoa.PostBackUrl = string.Concat(Constants.DEFAULT_PAGE, "?DeleteId=", kqHs.MaKQ);
            }
        }
    }


    protected void ddlPager_SelectedIndexChanged(Object sender, EventArgs e)
    {
        DropDownList ddlPager = grwKqHocSinh.BottomPagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = grwKqHocSinh.BottomPagerRow.FindControl("ltrCurrentPage") as Literal;

        ltrCurrentPage.Text = ddlPager.SelectedIndex.ToString();
        grwKqHocSinh.PageIndex = ddlPager.SelectedIndex;

        BindGridview();
    }

    protected void lbtFrist_Click(Object sender, EventArgs e)
    {
        DropDownList ddlPager = grwKqHocSinh.BottomPagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = grwKqHocSinh.BottomPagerRow.FindControl("ltrCurrentPage") as Literal;

        ddlPager.SelectedIndex = grwKqHocSinh.PageIndex = 0;
        ltrCurrentPage.Text = ddlPager.SelectedIndex.ToString();
        grwKqHocSinh.PageIndex = ddlPager.SelectedIndex;

        BindGridview();
    }

    protected void lbtPrevious_Click(Object sender, EventArgs e)
    {
        DropDownList ddlPager = grwKqHocSinh.BottomPagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = grwKqHocSinh.BottomPagerRow.FindControl("ltrCurrentPage") as Literal;

        ddlPager.SelectedIndex = grwKqHocSinh.PageIndex = grwKqHocSinh.PageIndex - 1;
        ltrCurrentPage.Text = ddlPager.SelectedIndex.ToString();
        grwKqHocSinh.PageIndex = ddlPager.SelectedIndex;
        
        BindGridview();
    }

    protected void lbtNext_Click(Object sender, EventArgs e)
    {
        DropDownList ddlPager = grwKqHocSinh.BottomPagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = grwKqHocSinh.BottomPagerRow.FindControl("ltrCurrentPage") as Literal;

        ddlPager.SelectedIndex = grwKqHocSinh.PageIndex = grwKqHocSinh.PageIndex + 1;
        ltrCurrentPage.Text = ddlPager.SelectedIndex.ToString();
        grwKqHocSinh.PageIndex = ddlPager.SelectedIndex;

        BindGridview();
    }

    protected void lbtLast_Click(Object sender, EventArgs e)
    {
        DropDownList ddlPager = grwKqHocSinh.BottomPagerRow.FindControl("ddlPager") as DropDownList;
        Literal ltrCurrentPage = grwKqHocSinh.BottomPagerRow.FindControl("ltrCurrentPage") as Literal;

        ddlPager.SelectedIndex = grwKqHocSinh.PageIndex = grwKqHocSinh.PageCount - 1;
        ltrCurrentPage.Text = ddlPager.SelectedIndex.ToString();
        grwKqHocSinh.PageIndex = ddlPager.SelectedIndex;

        BindGridview();
    }

    private void DisablePagingButton()
    { 
        if (grwKqHocSinh.PageIndex == grwKqHocSinh.PageCount - 1)
        {
            LinkButton lbtNext = grwKqHocSinh.BottomPagerRow.FindControl("lbtNext") as LinkButton;
            lbtNext.Enabled = false;
        }
        else if (grwKqHocSinh.PageIndex == 0)
        {
            LinkButton lbtPrevious = grwKqHocSinh.BottomPagerRow.FindControl("lbtPrevious") as LinkButton;
            lbtPrevious.Enabled = false;
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        BindDataForPager();
        DisablePagingButton();
    }
   
}